
const app = getApp();
import HttpRequst from '../../utils/httpRequst.js'
import API from '../../utils/config.js'
import WxValidate from '../../utils/WxValidate'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    validateTipIsShow: false,
    validateTipMsg: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initValidate();
  },
  formSubmit: function (e) {
    const params = e.detail.value;
    var that = this;
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0];
      this.setData({
        validateTipMsg: error.msg,
        validateTipIsShow: true
      })
      this._validateTipHide();
    } else {
      HttpRequst(true, API.teamAdd, 1, app.getUkey(), params, "POST", false, function (res) {
        setTimeout(function () {
          wx.showToast({
            title: '新增成功',
            icon: 'success',
            duration: 1000,
            success: function () {
              setTimeout(function () {
                wx.redirectTo({
                  url: '/pages/teamList/teamList',
                })
              }, 1000)
            }
          })
        })
      }) 
    }
  },
  /**
   * 2秒后隐藏错误提示信息
   */
  _validateTipHide: function () {
    var that = this;
    setTimeout(function () {
      that.setData({
        validateTipIsShow: false
      })
    }, 2000);
  },
  /**
   * 初始化验证插件
   */
  _initValidate: function () {
    const rules = {
      name:{
        required: true
      },
      mobile: {
        required: true,
        tel: true
      },
      identity: {
        required: true
      }
    };
    const messages = {
      name:{
        required: "请输入新增人员真实姓名",
      },
      mobile: {
        required: "请输入新增人员的手机号码",
        tel: "请输入正确的手机号码"
      },
      identity: {
        required: "请输入新增人员身份信息"
      }
    };
    this.WxValidate = new WxValidate(rules, messages);
  }
})